<!-- 编辑角色 -->
<template>
  <div class="role-edit">
    <div class="header">编辑角色</div>
    <el-form ref="roleForm" :model="roleForm" label-width="120px" :rules="rules">
      <el-form-item label="角色ID:">
        <el-input :value="roleForm.id" disabled></el-input>
      </el-form-item>
      <el-form-item label="角色名称:" prop="role_name">
        <el-input v-model="roleForm.role_name" placeholder="请输入角色名称"></el-input>
      </el-form-item>
      <el-form-item label="角色描述:">
        <el-input v-model="roleForm.desc" type="textarea" placeholder="请为当前角色添加新的描述~~~"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updateRoleEvent">立即更新</el-button>
        <el-button @click="$router.back()">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  inject: ["reload"],
  data() {
    return {
      roleForm: {},
      rules: {
        // 表单验证规则
        role_name: [
          {
            required: true,
            message: "请输入角色名称",
            trigger: "blur"
          },
          {
            min: 2,
            max: 10,
            message: "角色名称长度在 2 到 10 个字符",
            trigger: "blur"
          }
        ]
      }
    };
  },
  computed: {},
  created() {
    this.$http.getRoleById({ id: this.$route.params.id }).then(res => {
      if (res && res.data.code == 200) {
        this.roleForm = res.data.data;
      }
    });
  },
  methods: {
    updateRoleEvent() {
      // 表单验证
      this.$refs.roleForm.validate(valid => {
        if (valid) {
          // 调接口更新
          this.$http.updateRole(this.roleForm).then(res => {
            if (res && res.data.code === 200) {
              this.$message({
                type: "success",
                message: res.data.msg
              });
              this.roleForm = {}
              this.$router.push("/dashboard/system/role");
              this.reload();
            }
          });
        }
      });
    }
  }
};
</script>

<style lang="less">
.role-edit {
  width: 100%; 
  background: #fff;
  .header {
    padding: 15px 0 15px 20px;
    border-bottom: 1px solid #eee;
  }
  .el-form{
    padding: 40px 20px 80px 20px;
    width: 70%;
    margin: 0 auto;
  }
}
</style>